package cn.bdqn.web.controller.backend;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.io.output.WriterOutputStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.bdqn.entity.AppCategory;
import cn.bdqn.entity.AppInfo;
import cn.bdqn.entity.AppVersion;
import cn.bdqn.entity.DataDictionary;
import cn.bdqn.service.AppCategoryService;
import cn.bdqn.service.AppInfoService;
import cn.bdqn.service.AppVersionService;
import cn.bdqn.service.DictionaryService;
import cn.bdqn.utils.PageBean;

@Controller
@RequestMapping("/backendApp")
public class BackendAppController {
	@Autowired
	private AppCategoryService appCategoryService;
	@Autowired
	private DictionaryService dataDictionaryService;
	@Autowired
	private AppInfoService appInfoService;
	@Autowired
	private AppVersionService appVersionService;
	
	
		//主页面
		@RequestMapping("/disMain")
		public String disMain(){
			
			return "backend/main";
		}
		//显示商品信息
		@RequestMapping("/appInfoList")
		public String appInfoList(Model model,
								  @RequestParam(value="softwareName",required=false)String checkSoftwareName,
								  @RequestParam(value="flatform",required=false)String checkFlatform,
								  @RequestParam(value="level1",required=false)String checkLevel1,
								  @RequestParam(value="level2",required=false)String checkLevel2,
								  @RequestParam(value="level3",required=false)String checkLevel3,
								  @RequestParam(value="currentPage",required=false)String currentPage,
								  @RequestParam(value="pageSize",required=false)String pageSize
									){
			
		//APP状态
		List<DataDictionary> dicList=dataDictionaryService.findAllStatus("APP_STATUS");
		model.addAttribute("dicList", dicList);
		//所属平台
		List<DataDictionary> FlaList=dataDictionaryService.findAllStatus("APP_FLATFORM");
		model.addAttribute("FlaList", FlaList);
		//一级分类
		List<AppCategory> level1List=appCategoryService.findAllLevelOne();
		model.addAttribute("level1List", level1List);
		//appInfo
		Map<String, Object> map=new HashMap<>();
		map.put("softwareName", checkSoftwareName);
		map.put("status", 1);
		map.put("flatform", checkFlatform);
		map.put("level1", checkLevel1);
		map.put("level2", checkLevel2);
		map.put("level3", checkLevel3);
		map.put("currentPage", currentPage);
		map.put("pageSize", pageSize);
		
		PageBean<AppInfo> pageList = appInfoService.findAll(map);
		model.addAttribute("pageList", pageList);
		//数据回显
		model.addAttribute("mapList", map);
		//三级分类回显
		List<AppCategory> levelList2 = appCategory2(checkLevel1);
		List<AppCategory> levelList3 = appCategory2(checkLevel2);
		model.addAttribute("levelList2", levelList2);
		model.addAttribute("levelList3", levelList3);
		
		return "backend/appInfoList";
		}
		/**
		 * 三级联动
		 * @return
		 */
		@RequestMapping("/findLevel")
		public @ResponseBody List<AppCategory> appCategory(@RequestBody Map<String, String> map,Model model){
			
			String parentId=map.get("parentId");
			List<AppCategory> findLevelList = appCategoryService.findAllLevelByParentId(parentId);
			model.addAttribute("findLevelList", findLevelList);
			return findLevelList;
		} 
		
		
		public List<AppCategory> appCategory2(String parentId){
			List<AppCategory> findLevelList = appCategoryService.findAllLevelByParentId(parentId);
			return findLevelList;
		}
		/**
		 * 跳转到审核app页面
		 */
		@RequestMapping("/checkApp/{id}")
		public String checkApp(@PathVariable("id")Integer id,Model model){
			//判断是否上传最新版本
			Integer versionId=appInfoService.findVersionById(id);
			if(versionId==null){
				model.addAttribute("checkInfo", "必须上传最新版本");
				return "redirect:/backendApp/appInfoList";
			}else{
				AppInfo appInfo = appInfoService.findAppInfoById(id);
				model.addAttribute("appInfo", appInfo);
				AppVersion appVersion = appVersionService.findVersion(id);
				model.addAttribute("appVersion", appVersion);
				return "backend/banckedCheck";
			}
	
		}
		/**
		 * 审核通过
		 */
		@RequestMapping("/check2/{id}")
		public String check2(@PathVariable("id") Integer id){
			Integer num=appInfoService.updateStatusById2(id);
			if(num !=0){
				return "redirect:/backendApp/appInfoList";
			}
			return null;
		}
		/**
		 * 审核不通过
		 */
		@RequestMapping("/check3/{id}")
		public String check3(@PathVariable("id") Integer id){
			Integer num=appInfoService.updateStatusById3(id);
			if(num !=0){
				return "redirect:/backendApp/appInfoList";
			}
			return null;
		}
}
